import 'reflect-metadata';
import { config } from 'dotenv';
import { User, CrawlerTask, CrawledData } from '@weubi/shared'
// 加载测试环境变量
config({ path: '.env.test' });

// 设置测试环境变量
process.env.NODE_ENV = 'test';
process.env.DB_NAME = process.env.DB_NAME || 'weubi_crawler_test';
process.env.JWT_SECRET = process.env.JWT_SECRET || 'test-jwt-secret';
process.env.JWT_REFRESH_SECRET = process.env.JWT_REFRESH_SECRET || 'test-jwt-refresh-secret';

// 设置全局测试数据库配置
(global as any).__TEST_DB_CONFIG__ = {
  type: 'postgres',
  host: process.env.DB_HOST || 'localhost',
  port: parseInt(process.env.DB_PORT || '5433'),
  username: process.env.DB_USER || 'crawler',
  password: process.env.DB_PASSWORD || '123qwe',
  database: process.env.DB_NAME || 'weubi_crawler_test',
  synchronize: true, // 测试环境自动同步表结构
  logging: false, // 测试时关闭日志
  entities: [User, CrawlerTask, CrawledData],
  dropSchema: false, // 不要每次都删除schema，避免并发问题
};

// 增加测试超时时间
jest.setTimeout(30000);

// 全局错误处理
process.on('unhandledRejection', (reason, promise) => {
  console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});

process.on('uncaughtException', (error) => {
  console.error('Uncaught Exception:', error);
});
